/* 
 * This code is part of the smartlink project, see
 * 
 * 		http://code.google.com/p/smartlink/.
 * 
 * Copyright (C) 2008 Oliver Noelle
 * 
 * SmartLink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SmartLink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with SmartLink.  If not, see <http://www.gnu.org/licenses/>.
 **/
package org.smartlink;

import java.util.List;

import org.smartlink.provider.SmartLink;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * A list activity to pick a target (special case for creating a new link).
 */
public class PickTargetActivity extends ListActivity {

	private List<ResolveInfo> pickActivityList;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		ComponentName component = new ComponentName(this, LinkList.class);
		Intent intent = new Intent("org.smartlink.action.PICK_TARGET_ACTION", getIntent().getData());
		intent.addCategory(Intent.ALTERNATIVE_CATEGORY);
		PackageManager pm = getPackageManager();
		pickActivityList = pm.queryIntentActivityOptions(component, null, intent, 0);
		String[] labels = new String[pickActivityList.size()];
		int index = 0;
		for (ResolveInfo resolveInfo : pickActivityList) {
			labels[index] = resolveInfo.loadLabel(pm).toString();
			// TODO: add icon
			// resolveInfo.loadIcon(pm);
			index++;
		}
		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, labels));

		// default result: assume that pick activity will be canceled
		Bundle resultBundle = new Bundle(1);
		resultBundle.putString(Server.EXTRA_URI, getIntent().getData().toString());
		setResult(RESULT_CANCELED, null, resultBundle);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		ResolveInfo resolveInfo = pickActivityList.get(position);
		String packageName = resolveInfo.activityInfo.applicationInfo.packageName;
		String className = resolveInfo.activityInfo.name;
		ComponentName component = new ComponentName(packageName, className);
		Intent intent = new Intent("org.smartlink.action.PICK_TARGET_ACTION");
		intent.setComponent(component);
		startSubActivity(intent, SmartLink.PICK_TARGET_REQUEST_CODE);
	}

	/** result chaining */
	protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) {
		extras.putString(Server.EXTRA_URI, getIntent().getData().toString());
		setResult(resultCode, data, extras);
		finish();
	}

}
